/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-07-29 15:05:25
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-11-08 16:33:08
 * @FilePath: \gree-zst-web\src\router\routerHooks.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import type { Router } from "vue-router";
import { usePermissionStore } from "store/modules/permission";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

export function setupRouterHooks(router: Router): void {
  const whiteList = ["/login", "/loginCallback"];
  const permissionStore = usePermissionStore();
  router.beforeEach(async (to, from, next) => {
    console.log("routes", router.getRoutes());

    NProgress.start();
    document.title =
      (to.meta.title ? to.meta.title + " - " : "") +
      import.meta.env.VITE_APP_TITLE;

    if (whiteList.indexOf(to.path) > -1) {
      return next();
    }
    if (!permissionStore.hasGetPermission) {
      // 获取权限或者登陆
      await permissionStore.getPermission();
      return next(to.fullPath); // { path: to.fullPath, replace: true, query: to.query };
    } else {
      return next();
    }
  });

  router.afterEach(() => {
    NProgress.done();
  });
}
